package com.fyyg.service.jms;

import com.alibaba.fastjson.JSONObject;
import com.fyyg.common.util.date.YXDate;
import com.fyyg.dal.system.TBusiMobileMessageMapper;
import com.fyyg.interfaces.jms.JMSMessageService;
import com.github.pagehelper.StringUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.JmsException;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;


@Service("messageService")
public class JmsMessageServiceImpl implements JMSMessageService {

    private final static Logger logger = LogManager.getLogger(JmsMessageServiceImpl.class);

    @Autowired
    JmsTemplate jmsTemplate;
    @Autowired
    Destination destination;

    @Autowired
	private TBusiMobileMessageMapper mobileMessageMapper;
    
    @Override
    public void sendMessage(final String message) {
        try {
        	insertMobielMessage(message);
            jmsTemplate.send(destination, new MessageCreator() {
                public Message createMessage(Session session) throws JMSException {
                    return session.createTextMessage(message);
                }
            });
        } catch (JmsException e) {
            logger.info("发送错误\t"+e);
        }
    }
    
    
    public void insertMobielMessage(String meg){
    	try {
			JSONObject json = new JSONObject().parseObject(meg);
			String type = json.getString("type");
			if(!StringUtil.isEmpty(type) && type.equals("SMS")){
				String phone = json.getString("phone");
				String sednMessage = json.getString("message");
				String uid = json.getString("uid");
				String busiType = json.getString("busiType");
				String msgType = json.getString("msgType");
				String ip = json.getString("ip");
				
//				TBusiMobileMessage message = new TBusiMobileMessage();
//				message.setMobilenum(phone);
//				message.setMessage(sednMessage);
//				message.setUid(StringUtil.isEmpty(uid) ? 0:Integer.parseInt(uid));
//				message.setStatus(1);
//				message.setBusiType(StringUtil.isEmpty(busiType) ? 0:Integer.parseInt(busiType));
//				message.setMsgType(StringUtil.isEmpty(msgType) ? 0:Integer.parseInt(msgType));
//				message.setIp(StringUtil.isEmpty(ip)? "127.0.0.1":ip);
//				message.setSendtime(YXDate.getTime());
//				mobileMessageMapper.insert(message);
//				message=null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
}
